<?php

namespace Lib;

class ExceptionExt extends \Exception {

	public function __construct($message, $code = 0) {
		parent::__construct ( $message, $code );
	}

	/**
	 * 显示错误信息
	 * 如果$bTrace为true时显示错误追溯
	 *
	 * @param bool $bTrace        	
	 */
	public function showError($bTrace = true, $bHtml = true) {
		if ($bHtml) {
			$sHtml = '<html>';
			$sHtml .= '<head>';
			$sHtml .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
			$sHtml .= '<title>错误</title>';
			$sHtml .= '<style>
						th, td {font-size:12px;border:1px solid #86B9D6;padding:3px 5px;}
						table {border-collapse:collapse;line-height: 20px;}
						th {background:#f3f7ff;}
						h3 {line-height:24px;border:1px solid #86B9D6;background:#f3f7ff;padding:10px;font-size:24px;}
						</style>';
			$sHtml .= '</head>';
			$sHtml .= '<body><h3>';
			$sHtml .= $this->message;
			$sHtml .= '</h3>';
			if ($bTrace) {
				$aTraces = $this->getTrace ();
				$sHtml .= '<table>';
				$sHtml .= '<tr><th>文件</th><th>行</th><th>类</th><th>类</th><th>参数</th></tr>';
				$iStart = strlen ( ROOT );
				foreach ( $aTraces as $sRow ) {
					$sHtml .= '<tr>';
					$sHtml .= '<td>' . substr ( $sRow ['file'], $iStart ) . '</td>';
					$sHtml .= '<td>' . $sRow ['line'] . '</td>';
					$sHtml .= '<td>' . $sRow ['class'] . '</td>';
					$sHtml .= '<td>' . $sRow ['function'] . '</td>';
					if (count ( $sRow ['args'] )) {
						$sHtml .= '<td>' . var_export ( $sRow ['args'], true ) . '</td>';
					} else {
						$sHtml .= '<td></td>';
					}
					$sHtml .= '</tr>';
				}
				$sHtml .= '</table>';
			}
			$sHtml .= '</body></html>';
			echo $sHtml;
		} else {
			$sMessage = $this->message . "\n";
			if ($bTrace) {
				$aTraces = $this->getTrace ();
				foreach ( $aTraces as $sRow ) {
					$sMessage .= 'file: ' . $sRow ['file'] . "\n";
					$sMessage .= 'line: ' . $sRow ['line'] . "\n";
					$sMessage .= 'class: ' . $sRow ['class'] . "\n";
					$sMessage .= 'function: ' . $sRow ['function'] . "\n";
					if (count ( $sRow ['args'] )) {
						$sMessage .= 'args: ' . var_export ( $sRow ['args'], true ) . "\n";
					}
				}
			}
			echo $sMessage;
		}
		exit ();
	}
}